import requests
import uuid
import random

URL = "http://68.233.110.216:80/"
SIZE = 1024

def MakeFile(file):
    data = random.randbytes(SIZE)
    with open(file, 'wb') as file:
        file.write(data)
    return hash(data)

def upload(file_name):

    files = {"files": open(file_name, 'rb')}
    
    response = requests.post(URL + "upload", files=files)

    if response.status_code == 200:
        print(f"{file_name} uploaded successfully!")
    else:
        print(f"Failed to upload {file_name}, status code: {response.status_code} {response.text}")

def download(file_name, save_path):
    
    params = {"name": file_name}
    
    response = requests.get(URL + "download", params=params, stream=True)
    
    if response.status_code == 200:
        with open(save_path, "wb") as f:
            for chunk in response.iter_content(1024):
                f.write(chunk)
                
        print(f"File {file_name} downloaded successfully!")
    
    else:
        print(f"Failed to download {file_name}, status code: {response.status_code} {response.text}")

def check(_hash, file):
    with open(file, 'rb') as file:
        print("Hash confirmed!" if _hash == hash(file.read()) else "Hash missmatched!")
        
if __name__ == "__main__":
    for i in range(128):
        file = f"{str(uuid.uuid4())}.bin"
        _hash = MakeFile(file)
        upload(file)
        download(file, file)
        check(_hash, file)